package top.continew.ai.util.parse;

import org.springframework.ai.document.Document;
import org.springframework.ai.reader.TextReader;
import org.springframework.ai.reader.markdown.MarkdownDocumentReader;
import org.springframework.ai.reader.markdown.config.MarkdownDocumentReaderConfig;
import org.springframework.core.io.FileSystemResource;

import java.util.List;

/**
 * TextParse:
 *
 * @Author： Lonni
 * @Date： 2025/8/21  15:45
 */
public class MarkdownParse implements BaseParse {
    /**
     * 解析并切分文件
     *
     * @param filePath
     * @param fileName
     * @return
     */
    @Override
    public List<Document> renderDocment(String filePath, String fileName) {
        FileSystemResource fileSystemResource = new FileSystemResource(filePath);
        MarkdownDocumentReaderConfig config = MarkdownDocumentReaderConfig.builder()
                .withHorizontalRuleCreateDocument(true)     // 分割线创建新document
                .withIncludeCodeBlock(false)                // 代码创建新document
                .withIncludeBlockquote(false)               // 引用创建新document
                .withAdditionalMetadata("filename", fileSystemResource.getFilename())    // 每个document添加的元数据
                .build();


        MarkdownDocumentReader markdownDocumentReader = new MarkdownDocumentReader(fileSystemResource, config);
        List<Document> documents = markdownDocumentReader.read();
        return documents;

    }
}
